using System;
using System.Collections.Generic;
using System.Linq;

namespace TXK
{
    /// <summary>
    /// ULong转换类
    /// </summary>
    public class ULongLib
    {
        /// <summary>
        /// 字节数组中截取转成64位整型
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ulong GetULongFromByteArray(byte[] source, int start = 0, DataFormat type = DataFormat.ABCD)
        {
            byte[] array = ByteArrayLib.Get8ByteArray(source, start, type);
            return (array == null) ? 0 : BitConverter.ToUInt64(array, 0);
        }

        /// <summary>
        /// 将字节数组中截取转成64位整型数组
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ulong[] GetULongArrayFromByteArray(byte[] source, DataFormat type = DataFormat.ABCD)
        {
            ulong[] array = new ulong[source.Length / 8];
            for (int i = 0; i < source.Length / 8; i++)
            {
                array[i] = GetULongFromByteArray(source, 8 * i, type);
            }
            return array;
        }

        /// <summary>
        /// 将字符串转转成64位整型数组
        /// </summary>
        /// <param name="val"></param>
        /// <param name="spilt"></param>
        /// <returns></returns>
        public static ulong[] GetULongArrayFromString(string val, char spilt = ' ')
        {
            val = val.Trim();
            List<ulong> list = new List<ulong>();
            if (val.Contains(spilt))
            {
                string[] array = val.Split(spilt);
                string[] array2 = array;
                foreach (string text in array2)
                {
                    list.Add(Convert.ToUInt64(text.Trim()));
                }
            }
            else
            {
                list.Add(Convert.ToUInt64(val.Trim()));
            }
            return list.ToArray();
        }
    }
}
